package team.first.movieapiv2pro.controller;

import cn.hutool.core.convert.Convert;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.movieapiv2pro.domain.Staff;
import team.first.movieapiv2pro.dto.RequestStaff;
import team.first.movieapiv2pro.service.StaffService;
import team.first.movieapiv2pro.vo.ApiResponse;
import team.first.movieapiv2pro.vo.ApiResponsePaging;

import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapibootv2.controller
 * @Date: 2021/11/23 23:17
 * @Description：
 */
@RestController
public class StaffController {

    @Autowired
    private StaffService staffService;

    public static int totalRow;

    /**
     * 2.4 获取指定编号的演员信息
     * 接口地址：GET /api/v2/staff/{staffId}
     */
    @ApiOperation("1.4 GET: 获取指定编号的影人信息")
    @GetMapping("/api/v2/staff/{staffId}")
    public ApiResponse<Staff> getStaffById(@PathVariable("staffId") Integer staffId) {
        ApiResponse<Staff> apiResponse = new ApiResponse<>();
        Staff staff = staffService.getById(staffId);
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(staff);
        return apiResponse;
    }

    /**
     * 2.5根据条件分页获取的影人信息
     * 接口地址:POST /api/v2/staffs
     */
    @PostMapping("/api/v2/staffs")
    @ApiOperation("1.5 POST： 根据条件分页获取的影人信息")
    public ApiResponsePaging<List<Staff>> getStuff(RequestStaff requestStaff) {
        ApiResponsePaging<List<Staff>> apiResponsePaging = new ApiResponsePaging<>();

        apiResponsePaging.setCode(200).setSuccess(true).setMessage("请求成功").setData(staffService.getStaff(requestStaff)).setTotalRow(totalRow);

        if (requestStaff.getPagerSize() != null) {
            apiResponsePaging.setPageSize(requestStaff.getPagerSize())
                    .setTotalPage(Convert.toInt(Math.ceil((double) totalRow / requestStaff.getPagerSize())));
        }
        if (requestStaff.getPageNumber() != null) {
            apiResponsePaging.setPageNumber(requestStaff.getPageNumber());
        }

        return apiResponsePaging;
    }
}
